<template>
  <div class="container">
    <!-- 卡片主体区域 -->
    <el-card class="box-card">
      <el-row :gutter="10">
        <el-col :span="18">
          <!-- 表单 -->
          <el-col :span="6">
            <el-form ref="subjectForm" :model="queryList" :inline="true">
              <el-form-item label="学科名称">
                <el-input
                  v-model="queryList.subjectName"
                  @clear="getSubjectList"
                ></el-input>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="18">
            <el-button size="small" class="colInput" @click="clearSub"
              >清除</el-button
            >
            <el-button
              size="small"
              class="colInput"
              type="primary"
              @click="getSubjectList"
              >搜索</el-button
            >
          </el-col>
        </el-col>
        <el-col :span="6">
          <el-button
            class="colright"
            type="success"
            icon="el-icon-edit"
            @click="addSubjects"
            >新增学科</el-button
          >
        </el-col>
      </el-row>
      <!-- 消息文案 -->
      <el-alert
        :title="`数据一共 ${counts} 条`"
        :closable="false"
        type="info"
        show-icon
      >
      </el-alert>
      <!-- 列表区域 -->
      <template>
        <el-table :data="subjectList" style="width: 100%">
          <el-table-column type="index" label="序号" width="50">
          </el-table-column>
          <el-table-column prop="subjectName" label="学科名称" width="150">
          </el-table-column>
          <el-table-column prop="username" label="创建者"> </el-table-column>
          <el-table-column prop="addDate" label="创建日期"> </el-table-column>
          <el-table-column prop="isFrontDisplay" label="前台是否显示">
            <template slot-scope="scope">
              {{ scope.row.isFrontDisplay === 1 ? "是" : "否" }}
            </template>
          </el-table-column>
          <el-table-column prop="twoLevelDirectory" label="二级目录">
          </el-table-column>
          <el-table-column prop="tags" label="标签"> </el-table-column>
          <el-table-column prop="totals" label="题目数量"> </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="small"
                type="text"
                @click="
                  $router.push(
                    `directorys?id=${scope.row.id}&name=${scope.row.subjectName}`
                  )
                "
                >学科分类</el-button
              >
              <el-button
                size="small"
                type="text"
                @click="
                  $router.push(
                    `tags?id=${scope.row.id}&name=${scope.row.subjectName}`
                  )
                "
                >学科标签</el-button
              >
              <el-button size="small" type="text" @click="editSub(scope.row)"
                >修改</el-button
              >
              <el-button size="small" type="text" @click="deleteSub(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- /列表区域 -->
      <!-- 分页区域 -->
      <pagination-add
        :page="queryList.page"
        :pagesize="queryList.pagesize"
        :counts="counts"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      />
      <!-- /分页区域 -->
    </el-card>
    <!-- 新增学科  弹框-->
    <el-dialog
      title="新增学科"
      :visible.sync="addDialogVisible"
      width="30%"
      close="resetSub"
    >
      <!-- 主体区域 -->
      <el-row>
        <el-col>
          <el-form
            :model="addSubjectsForm"
            ref="addSubjectsFromRef"
            :rules="addSubjectsFromRules"
            label-width="80px"
          >
            <el-form-item label="学科名称" prop="subjectName">
              <el-input
                v-model="addSubjectsForm.subjectName"
                placeholder="请输入名称"
              ></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <span class="sub">是否显示</span>
        <!-- 开关 -->
        <el-switch
          v-model="subSwitch"
          active-color="#13ce66"
          inactive-color="#ff4949"
        >
        </el-switch>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSubForm">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改弹框 -->
    <el-dialog
      title="新增学科"
      :visible.sync="editDialogVisible"
      width="30%"
      close="editResetSub"
    >
      <!-- 主体区域 -->
      <el-row>
        <el-col>
          <el-form
            :model="editSubjectsForm"
            ref="editSubjectsFromRef"
            :rules="editSubjectsFromRules"
            label-width="80px"
          >
            <el-form-item label="学科名称" prop="subjectName">
              <el-input v-model="editSubjectsForm.subjectName"></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <span class="sub">是否显示</span>
        <!-- 开关 -->
        <el-switch
          v-model="editSubjectsForm.isFrontDisplay"
          active-color="#13ce66"
          inactive-color="#ff4949"
        >
        </el-switch>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSubForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, add, update, remove } from "../../api/hmmm/subjects";
import PaginationAdd from "../components/Pagination/pagination-add";

export default {
  name: "Subjects",
  data() {
    return {
      // 修改的id
      userId: "",
      // 学科列表
      subjectList: [],
      // 修改默认值表单
      editSubjectsForm: {
        subjectName: "",
        isFrontDisplay: ""
      },
      // 修改的验证
      editSubjectsFromRules: {
        subjectName: [
          { required: true, message: "请输入学科", trigger: "blur" },
          { min: "3", max: "8", message: "长度在3-8个字符", trigger: "blur" }
        ]
      },
      // 添加的验证
      addSubjectsFromRules: {
        subjectName: [
          { required: true, message: "请输入学科", trigger: "blur" },
          { min: "3", max: "8", message: "长度在3-8个字符", trigger: "blur" }
        ]
      },
      // 获取学科列表的请求参数
      queryList: {
        page: 1,
        pagesize: 10,
        subjectName: ""
      },
      // 学科列表总条数
      counts: 10,
      // 添加的输入框数据
      addSubjectsForm: {
        subjectName: ""
      },
      // 添加弹框的显示与隐藏
      addDialogVisible: false,
      // 修改弹框的显示与隐藏
      editDialogVisible: false,
      // 是否显示按钮的显示与隐藏
      subSwitch: true
    };
  },
  components: {
    PaginationAdd
  },
  created() {
    this.getSubjectList();
  },
  computed: {
    isFrontDisplay() {
      if (this.subSwitch) {
        return 1;
      } else {
        return 0;
      }
    }
  },
  methods: {
    // 获取学科的列表数据
    async getSubjectList() {
      try {
        const { data } = await list(this.queryList);
        data.items.forEach(item => {
          item.addDate = this.$dayjs(item.addDate).format(
            "YYYY-MM-DD hh:mm:ss"
          );
        });
        this.subjectList = data.items;
        this.counts = data.counts;
        console.log(data);
      } catch (e) {
        this.$message.error("请求数据失败");
      }
    },
    // 点击新增学科弹框
    addSubjects() {
      this.addDialogVisible = true;
    },
    //  重置输入框
    resetSub() {
      this.$refs.addSubjectsFromRef.resetFields();
    },
    //  清除按钮
    clearSub() {
      this.queryList.subjectName = "";
      this.getSubjectList();
    },
    // 添加学科
    async addSubForm() {
      try {
        await add({
          subjectName: this.addSubjectsForm.subjectName,
          isFrontDisplay: this.isFrontDisplay
        });
        this.$message.success("新增学科成功");
        this.addDialogVisible = false;
        this.getSubjectList();
      } catch (err) {
        this.$message.error("新增学科失败");
      }
    },
    // 修改
    editSub(info) {
      this.editSubjectsForm.subjectName = info.subjectName;
      if (info.isFrontDisplay === 1) {
        this.editSubjectsForm.isFrontDisplay = true;
      } else {
        this.editSubjectsForm.isFrontDisplay = false;
      }
      this.userId = info.id;
      this.editDialogVisible = true;
    },
    // 修改学科
    async editSubForm() {
      if (this.editSubjectsForm.isFrontDisplay === true) {
        this.editSubjectsForm.isFrontDisplay = 1;
      } else {
        this.editSubjectsForm.isFrontDisplay = 0;
      }
      try {
        await update({
          id: this.userId,
          subjectName: this.editSubjectsForm.subjectName,
          isFrontDisplay: this.editSubjectsForm.isFrontDisplay
        });
        this.$message.success("修改学科成功");
        this.editDialogVisible = false;
        this.getSubjectList();
      } catch (err) {
        this.$message.error("修改学科失败");
      }
    },
    //  修改清空
    editResetSub() {
      this.$refs.editSubjectsFromRef.resetFields();
    },
    //  删除
    async deleteSub(row) {
      const configResult = await this.$confirm(
        "此操作将永久删除该学科, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (configResult !== "confirm") {
        return this.$message("您取消了删除操作");
      }
      try {
        const { data } = await remove(row);
        console.log(data);
        this.$message.success("删除成功");
        this.getSubjectList();
      } catch (err) {
        this.$message.error("删除失败");
      }
    },
    // 一页多少条
    handleSizeChange(newSize) {
      this.queryList.pagesize = newSize;
      this.getSubjectList();
    },
    // 多少页
    handleCurrentChange(newPage) {
      this.queryList.page = newPage;
      this.getSubjectList();
    }
  }
};
</script>

<style scoped lang="less">
.container {
  .colInput {
    height: 35px;
  }
  .colright {
    float: right;
    height: 35px;
  }
  .sub {
    font-size: 14px;
    font-weight: 700;
    margin-right: 10px;
    margin-left: 10px;
  }
  .el-alert {
    margin-bottom: 20px;
  }
  .el-pagination {
    float: right;
    margin-top: 20px;
  }
}
</style>
